package com.gets.web.controller;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gets.web.model.StockEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * Created by sjj on 2015/10/24 0024.
 */
@Controller
public class ImageSetController {

    //002797
    @ResponseBody
    @RequestMapping(value = "/gets", method = RequestMethod.GET)
    public StockEntity getImageSetList(String code) {
        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setTimeout(Integer.MAX_VALUE);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        HtmlPage rootPage = null;
        StockEntity stockEntity = null;
        try {
            rootPage = webClient.getPage("http://quote.eastmoney.com/sz" + code + ".html");
            System.out.println(rootPage.getTitleText().replaceAll("[^0-9.(% ]", ""));
            String all = rootPage.getTitleText().replaceAll("[^0-9.( ]", "");
            stockEntity = new StockEntity();
            stockEntity.setMoney(all.split(" ")[0]);
            stockEntity.setIncrease(all.split(" ")[1].split("\\(")[0]);
            stockEntity.setPercent(all.split(" ")[1].split("\\(")[1]);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stockEntity;
    }

}